<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\StoreBargainUser;
use App\Traits\Controllers\AdminTraits;
use App\Traits\Controllers\IndexTraits;
use App\Traits\Controllers\ShowTraits;
use App\Transformers\Store\StoreBargainUserHelpTransformer;
use App\Transformers\Store\StoreBargainUserTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class StoreBargainUserController extends Controller
{
    use AdminTraits;

    use IndexTraits;

    public $model = StoreBargainUser::class;

    public $transformer = StoreBargainUserTransformer::class;

    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        //获取传过来的时间类型
        $time = $request->input('time');
        //获取开始时间和结束时间
        $start = $request->input('start');
        $stop = $request->input('stop');

        $data = $this
            ->getQuery()
            ->whereHas('storeBargain', function ($query) use ($time,$start,$stop) {
                //自定义时间
                if(!empty($start) && !empty($stop)){
                    $query->whereBetween('start_at',[$start,$stop]);
                }

                //搜索今天数据
                if ($time == "today") {
                    $query->whereBetween('start_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()]);
                }

                //搜索昨天数据
                if ($time == "yesterday") {
                    $query->whereDay('start_at', [Carbon::now()->yesterday(), Carbon::now()->startOfDay()]);
                }

                //搜索最近一周数据
                if ($time == "week") {
                    $query->whereBetween('start_at', [Carbon::now()->subDays(7), Carbon::now()->subDays(0)]);
                }

                //搜索最近三十天数据
                if ($time == "thirty") {
                    $query->whereBetween('start_at', [Carbon::now()->subDays(30), Carbon::now()->subDays(0)]);
                }

                //搜索本月数据
                if ($time == "month") {
                    $query->whereBetween('start_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()]);
                }

                //搜索本年数据
                if ($time == "year") {
                    $query->whereBetween('start_at', [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()]);
                }
            })
            ->pimp()//自动转换where条件
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }

    /**
     * 查看详情
     */
    public function show($id)
    {
        $data = $this->getQuery()
            ->find($id)
            ->userHelp()
            ->paginate($this->getPageLimit());
        return $this->response->paginator($data, new StoreBargainUserHelpTransformer());
    }
}
